#include <unistd.h>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <sys/wait.h>

int main()
{
    int pipefd[2]={0};
    int retpipe=pipe(pipefd);
    if(retpipe<0)return -1;
    pid_t id=fork();
    if(id<0)
    {
        return -2;
    }
    else if(id==0)
    {
        //child  //父写子读
        close(pipefd[1]);
        char buffer[20]={0};
        while(true)
        {
             ssize_t n=read(pipefd[0],buffer,sizeof(buffer));
             if(n>0)
             {
                buffer[n]='\0';
                printf("%s\n",buffer);
             }
             else if(n==0)
             break;
        }
        close(pipefd[0]);
        exit(0);
    }
    else
    //farther
    close(pipefd[0]);
    const char*s="i am farther";
    ssize_t n=write(pipefd[1],s,strlen(s));
    close (pipefd[1]);
    if(n<0)return -1;
    pid_t ret=waitpid(id,NULL,0);
    if(ret==-1)return -2;
    return 0;
}